﻿function RegisterController($scope, service, $location, $http) {
    $scope.registerInfo = {
        UserAccess: {
            Email: "",
            Password: ""
        },
        Company: {
            UserAccessId: "",
            Name: "",
            SiteName: "",
            IsOrganization: false
        }
    };
    function onCreate() {
        if (service.registerInfo != null) {
            $scope.registerInfo = service.registerInfo;
        }
    }

    onCreate();
    $scope.confirmEmail = function () {
        var api = "api/login/checkemail";
        $http.post(api, $scope.registerInfo.UserAccess)
            .success(function () {
                service.registerInfo = $scope.registerInfo;
                $location.path("/RegisterCompleteInformation");
            }).error(function (data) {
                console.log(data);
            });
    };

    $scope.registerConfirm = function () {
        service.registerInfo = $scope.registerInfo;
        $location.path("/RegisterConfirm");
    };

    $scope.apply = function () {
        var api = "api/useraccess";
        $http.post(api, $scope.registerInfo.UserAccess)
            .success(function (id) {
                $scope.registerInfo.Company.UserAccessId = id;
                service.registerInfo = $scope.registerInfo;
                registerConpany($scope.registerInfo.Company);
            }).error(function (data) {
                console.log(data);
            });
    };

    function registerConpany(companyInfo) {
        var api = "api/company";
        $http.post(api, companyInfo)
            .success(function () {
                service.registerInfo = $scope.registerInfo;
                $location.path("/RegisterSuccess");
            }).error(function (data) {
                console.log(data);
            });
    }
}